/*
   If you plan to use this store with a widget that is a Dojo Data consumer, then you need
   to use dojo.data.ObjectStore wrapper around this store.
   More info: http://dojotoolkit.org/reference-guide/dojo/data/ObjectStore.html
*/


define(
   [
      'dojo/_base/declare',
      'dojo/_base/lang',
      'dojo/topic',
      'dojo/Evented',
      'noBusinessBase/store/JsonRest'
   ],
   function(declare, lang, topic, Evented, Store)
   {
      return declare([Store, Evented],
      {
         // Member Variables
         mDistributeName: null,
         mEmitter: false,
         
         // Constuctor
         constructor:function (args)
         {
            // Call Super
            dojo.safeMixin(this, args);
         },
         postscript: function(args)
         {
            // Called after constructors
            this.inherited(arguments);

            // If this store has no distribute name, don't do anything else
            if(!this.mDistributeName)
            {
               console.error('noBusinessBase dojo library error: JsonRestDistribute store could not subscribe to signals beacuse no DistributeName was set.');
               return;
            }

            if(this.mEmitter)
               return;

            this.own(
               topic.subscribe(this._getSignalName('onChange'), lang.hitch(this, this.onChangeSignalCaught)),
               topic.subscribe(this._getSignalName('onCreate'), lang.hitch(this, this.onCreateSignalCaught)),
               topic.subscribe(this._getSignalName('onRemove'), lang.hitch(this, this.onRemoveSignalCaught))
            );
         },
         onChangeSignalCaught: function(params)
         {
            this.onChange(params.pObject);
         },
         onCreateSignalCaught: function(params)
         {
            this.onCreate(params.pObject);
         },
         onRemoveSignalCaught: function(params)
         {
            this.onRemove(params.pObject);
         },
         emitOnChange: function(pObject)
         {
            topic.publish(this._getSignalName('onChange'), {
               pObject:             pObject
            });
         },
         emitOnCreate: function(pObject)
         {
            topic.publish(this._getSignalName('onCreate'), {
               pObject:             pObject
            });
         },
         emitOnRemove: function(pObject)
         {
            topic.publish(this._getSignalName('onRemove'), {
               pObject:             pObject
            });
         },
         onChange: function(pObject)
         {
            this.inherited(arguments);

            if(this.sendNotify)
               this.sendNotify(pObject, pObject.Id);
         },
         onCreate: function(pObject)
         {
            this.inherited(arguments);

            if(this.sendNotify)
               this.sendNotify(pObject, pObject.Id);
         },
         onRemove: function(pObject)
         {
            this.inherited(arguments);

            if(this.sendNotify)
               this.sendNotify(undefined, pObject.Id);
         },
         _getSignalName: function(functionName)
         {
            return 'noBusinessBase:JsonRestDistribution:' + this.mDistributeName + ':' + functionName;
         }
      });
   }
);
